package com.loveplusplus.update;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import android.app.Activity;
import android.content.Intent;

import java.io.File;

public class AndroidAutoUpdateModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
    
    private static ReactApplicationContext context;
    public AndroidAutoUpdateModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }
    @ReactMethod
    public void checkDownloadFile (String apkName, Promise promise){
        File dir = StorageUtils.getCacheDirectory(this.getReactApplicationContext());
        if( dir == null ) {
            promise.reject("找不到缓存目录");
            return;
        }
        File apkFile = new File(dir, apkName);
        if (apkFile.exists()){
            promise.resolve(apkFile.getAbsolutePath());
        }else {
            promise.resolve(null);
        }
    }
    @ReactMethod
    public void goToDownload (String url, String apkName){
        final Activity activity = getCurrentActivity();
        Intent intent = new Intent(activity.getApplicationContext(), DownloadService.class);
        intent.putExtra(Constants.APK_DOWNLOAD_URL, url);
        intent.putExtra(Constants.APK_DOWNLOAD_NAME, apkName);
        activity.startService(intent);
    }
     
    @ReactMethod
    public void installApk (String url){
       ApkUtils.installAPk(this.getReactApplicationContext(), url);
    }
    public static void sendProgress(int msg) {
        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("LUOKUN_LOAD_PROGRESS", msg);
    }
    public static void sendError(String msg) {
        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("LUOKUN_LOAD_ERROR", msg);
    }
    public static void sendDone(String msg) {
        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("LUOKUN_LOAD_DONE", msg);
    }
    @Override
    public void onHostResume() {

    }
    @Override
    public void onHostPause() {

    }

    @Override
    public void onHostDestroy() {
    }

    @Override
    public String getName() {
        return "RNAndroidAutoUpdate";
    }
}
